﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using Yoozoo.Gameplay.City;

namespace Yoozoo.Gameplay.CityEditor
{
    public struct DoubleVector3
    {
        public double x;
        public double y;
        public double z;

        public DoubleVector3(Vector3 vec)
        {
            x = Math.Round(vec.x, 2);
            y = Math.Round(vec.y, 2);
            z = Math.Round(vec.z, 2);
        }
    }
    public class HudPointData
    {
        public int hudId;
        public DoubleVector3 position;
        public DoubleVector3 rotation;
        public DoubleVector3 scale;
    }
    public class RoomExportData
    {
        public int id;
        public string name;
        public List<HudPointData> hudPointList;
        public DoubleVector3 jumpPosition;
        public DoubleVector3 hudScale;
        public DoubleVector3 virtualCameraPos;
        public DoubleVector3 virtualCameraRot;
        public DoubleVector3 portraitVirtualCameraPos;
        public DoubleVector3 portraitVirtualCameraRot;
    }
    
    [Serializable]
    public class CityRoomListEditorData
    {
        public List<RoomExportData> roomDataList;
        public List<RoomExportData> eventDataList;
    }
    public class CityRoomDataEditor : MonoBehaviour
    {
        // Start is called before the first frame update
        public void Save(string path)
        {
#if UNITY_EDITOR
            Debug.Log("开始导出！");
            // 遍历子节点中的RoomData进行数据导出
            var roomList = transform.GetComponentsInChildren<RoomData>();
            
            if (roomList!=null)
            {
                CityRoomListEditorData exportData = new CityRoomListEditorData();
                exportData.roomDataList = new List<RoomExportData>();
                exportData.eventDataList = new List<RoomExportData>();
                for (int i = 0; i < roomList.Length; i++)
                {
                    RoomExportData roomExportData = new RoomExportData();
                    var roomData = roomList[i];
                    roomExportData.id = roomData.rID;
                    roomExportData.name = roomData.name;
                    roomExportData.hudScale =  new DoubleVector3(roomData.editorScale);
                    if (roomData.currentVirtualCamera)
                    {
                        roomExportData.virtualCameraPos = new DoubleVector3(roomData.currentVirtualCamera.transform.localPosition);
                        roomExportData.virtualCameraRot = new DoubleVector3(roomData.currentVirtualCamera.transform.localEulerAngles);
                    }
                    if (roomData.portraitVirtualCamera)
                    {
                        roomExportData.portraitVirtualCameraPos = new DoubleVector3(roomData.portraitVirtualCamera.transform.localPosition);
                        roomExportData.portraitVirtualCameraRot = new DoubleVector3(roomData.portraitVirtualCamera.transform.localEulerAngles);
                    }
                    
                    roomExportData.jumpPosition = new DoubleVector3(roomData.jumpPoint.localPosition);
                    roomExportData.hudPointList = new List<HudPointData>();

                    for (int j = 0; j < roomData.hudPoints.Count; j++)
                    {
                        HudPointData hudPointData = new HudPointData();
                        var hudData = roomData.hudPoints[j];
                        hudPointData.hudId = hudData.hID;
                        hudPointData.position = new DoubleVector3(hudData.hPos.localPosition);
                        hudPointData.rotation = new DoubleVector3(hudData.hPos.localEulerAngles);
                        hudPointData.scale = new DoubleVector3(hudData.hPos.localScale);
                        roomExportData.hudPointList.Add(hudPointData);
                    }
                    
                    if (roomData.rID == 0)
                    {
                        exportData.eventDataList.Add(roomExportData);
                    }
                    else
                    {
                        exportData.roomDataList.Add(roomExportData);
                    }
                    
                }
                var jsonText = LitJson.JsonMapper.ToJson(exportData);
                var luaText = JsonToLua.ConvertLua(jsonText);
                FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
                using (StreamWriter writer = new StreamWriter(fs))
                {
                    writer.Write(luaText);
                    writer.Flush();
                    writer.Close();
                    fs.Close();
                    Debug.Log("导出结束！");
                    EditorUtility.DisplayDialog("导出成功", "导出成功！", "确定");
                }
                AssetDatabase.Refresh();
            }
            
#endif
        }
    }
}

